const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/api/users') {
    // const users = [
    //   { id: 1, name: 'John' },
    //   { id: 2, name: 'Jane' },
    //   { id: 3, name: 'Alice' },
    // ];
    const users = {
        success:"true",
        data:[
          { id: 1, name: 'John' },
          { id: 2, name: 'Jane' },
          { id: 3, name: 'Alice' },
        ],
        code:0,
        msg:"请求成功！"
    }
    res.setHeader('Content-Type', 'application/json');
    
    // 设置允许的请求来源  配置的时候不能加 / 
    res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:5173');
    
    res.end(JSON.stringify(users));

  }
  else if (req.url === '/download/latest') {
    // const users = [
    //   { id: 1, name: 'John' },
    //   { id: 2, name: 'Jane' },
    //   { id: 3, name: 'Alice' },
    // ];
    const users = {
        success:"true",
        data:"1.0.7",
        code:0,
        msg:"请求成功！"
    }
    res.setHeader('Content-Type', 'application/json');
    
    // 设置允许的请求来源  配置的时候不能加 / 
    res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:5173');
    
    res.end(JSON.stringify(users));

  } else {
    res.statusCode = 404;
    res.end();
  }
});

const port = 3000;
server.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});